export default {
  //开始游戏
  resetGame(){
    this.noClick = this.rows * this.columns
    this.time = 0
    this.mines = this.totalMines == 0 ? 10 : this.totalMines
    this.totalMines = this.mines
    this.showMenu = false
    this.gameState = 'waiting'
    this.item = new Array(this.rows)
    for(let i = 0 ; i < this.rows ; i++){
      this.item[i] = new Array()
      for(let j = 0 ; j < this.columns ; j++){
        let obj = {}
        obj.ismine = false
        obj.flag = false
        obj.clicked = false
        obj.qMark = false
        obj.mines = 0
        this.item[i].push(obj)
      }
    }
    this.minesGenerate()
    this.initialNums()
    this.$forceUpdate()
  },
  //鼠标按下开始按钮
  addClass(){
    this.$refs.startButton.classList.add('clicks')
    //省事
    // location.reload();
    this.resetGame()
  },
  //鼠标弹起开始按钮
  removeClass(){
    this.$refs.startButton.classList.remove('clicks')
  },
  //点击未松开单元格时触发
  clickItem(i,j){
    this.buttonImg = require('../assets/img/'+'click.png')
    if(i<0 || j<0 && this.showCover == true){
      return
    }
    //按下鼠标左键
    if(event.button == '0' && this.item[i][j].flag == false){
      this.clickType = 0
      if(this.item[i][j].clicked == false){
        this.clicking = true
      }
    //按下鼠标中键
    }else if(event.button == '1' && this.item[i][j].flag == false){
      this.clickType = 1
      this.clicking = true
      this.hiddenAround([i,j])
    //按下鼠标右键
    }else if(event.button == '2'){
      this.clickType = 2
      if(this.item[i][j].clicked === false){
        let flag = this.item[i][j].flag
        let qMark = this.item[i][j].qMark
        if(flag == false && qMark == false){
          this.mines--
          this.item[i][j].flag = true
        }else if(flag == true){
          this.mines++
          this.item[i][j].qMark = true
          this.item[i][j].flag = false
        }else{
          this.item[i][j].qMark = false
        }
      }
    }
  }, 
  //松开鼠标时触发
  clickUp(i,j){
    if(this.showCover){
      return
    }
    this.clickType = null
    this.clicking = false
    this.buttonImg = require('../assets/img/'+'xiaolian.png')
    if( i>=0 && j>=0 && this.item[i][j].flag == true){
      return
    }
    //鼠标左键弹起
    if(event.button == '0' && i>=0 && j>=0){
      if(this.moving){
        this.MoveUp('.out')
        this.moving = false
      }
      if(this.item[i][j].clicked === false){
        this.item[i][j].clicked = true
        this.noClick--
      }
      if(this.gameState !== 'playing'){
        this.gameState = 'playing'
      }
      this.clickJudge(i,j)
    //鼠标中键键弹起
    }else if(event.button == '1'){
      this.hiddenAround([-1])
      if(i>=0 && j>=0 && this.item[i][j].clicked === true){
        this.showAround(i,j)
      }
      
    }
  },
  //检查是否游戏成功
  check(){
    let result = true
    for(i=0;i<this.rows;i++){
      for(j=0;j<this.columns;j++){
        if(this.item[i][j].ismine){
          let item = this.item[i][j]
          if(!item.flag){
            result = false
          }
        }
      }
    }
    return result
  },
  //点击时触发的判断
  clickJudge(x,y){
    if(this.item[x][y].ismine){
      this.item[x][y].mines = 10
      this.gameState = 'fail'
    }else if(this.item[x][y].mines == 0){
      this.showBlankItem(x,y)
    }
  },
  //鼠标移入单元格时触发
  mouseOver(i,j){
    if(this.clicking === true){
      if(this.clickType === 1){
        this.hiddenAround([i,j])
      }
    }
    if(this.openCheat){
      let dot = document.body.nextElementSibling
      if(this.item[i][j].ismine){
        dot.style.backgroundColor = '#fff'
      }else{
        dot.style.backgroundColor = '#000'
      }
    }
  },
  //隐藏周围的方块
  hiddenAround(arr){
    let oldIndex = this.currentIndex
    this.currentIndex = arr
    let newIndex = this.currentIndex
    if(newIndex.toString() !== oldIndex.toString()){
      if(oldIndex[0] !== -1){
        for(let x = oldIndex[0] - 1;x >= oldIndex[0] - 1 && x <= oldIndex[0] + 1 ; x++){
          for(let y = oldIndex[1] - 1;y >= oldIndex[1] - 1 && y <= oldIndex[1] + 1 ; y++){
            if(x >= 0 && y >= 0 && x < this.rows && y < this.columns){
              // console.log(this.$refs.itemCover[x*this.columns + y])
              document.querySelectorAll('.item')[x*this.columns + y].lastChild.removeAttribute("style");
            }
          } 
        }
      }
      if(newIndex[0] !== -1){
        for(let x = newIndex[0] - 1;x >= newIndex[0] - 1 && x <= newIndex[0] + 1 ; x++){
          for(let y = newIndex[1] - 1;y >= newIndex[1] - 1 && y <= newIndex[1] + 1 ; y++){
            if(x >= 0 && y >= 0 && x < this.rows && y < this.columns && this.item[x][y].flag == false){
              document.querySelectorAll('.item')[x*this.columns + y].lastChild.style.opacity = '0'
            }
          } 
        }
      }
    }
    
  },
  //随机给一个单元格设置为雷
  random(){
    let x = parseInt(Math.random() * this.rows)
    let y = parseInt(Math.random() * this.columns)
    if(this.item[x][y].ismine === true){
      this.random()
    }else{
      this.item[x][y].ismine = true
    }
  },
  //生成雷的坐标
  minesGenerate(){
    let arr = []
    for(let i = 0 ; i < this.mines ; i++ ){
      this.random()
    }
  },
  //计算周围雷数
  minesCheck(i,j){
    if(this.item[i][j].ismine){
      return null
    }else{
      let num = 0
      for(let x = i - 1;x >= i - 1 && x <= i + 1 ; x++){
        for(let y = j - 1;y >= j - 1 && y <= j + 1 ; y++){
          if(x>=0 && y>=0 && x<this.rows && y<this.columns){
            if(this.item[x][y].ismine){
              num++
            }
          }
        } 
      } 
      return num
    }
  },
  //展示周围的单元格
  showAround(i,j){
    let num = 0
    // let mines = this.mineCheck
    for(let x = i - 1;x >= i - 1 && x <= i + 1 ; x++){
      for(let y = j - 1;y >= j - 1 && y <= j + 1 ; y++){
        if(x>=0 && y>=0 && x<this.rows && y<this.columns){
          if(this.item[x][y].flag == true){
            num++
          }
        }
      }
    }
    if(this.item[i][j].mines <= num){ 
      for(let x = i - 1;x >= i - 1 && x <= i + 1 ; x++){
        for(let y = j - 1;y >= j - 1 && y <= j + 1 ; y++){
          if(x>=0 && y>=0 && x<this.rows && y<this.columns){
            if(this.item[x][y].flag === true && this.item[x][y].ismine === false){
              this.item[x][y].mines = 9
              this.item[x][y].clicked = true
            }
            if(this.item[x][y].flag === false && this.item[x][y].clicked === false){
              
              this.item[x][y].clicked = true
              this.noClick--
              this.clickJudge(x,y)
            }
          }
        }
      }
    }
  },
  //递归展示周围附近没有雷的单元格
  showBlankItem(i,j){
    for(let x = i - 1 ; x <= i + 1 ; x++ ){
      for(let y = j - 1 ; y <= j + 1 ; y++ ){
        if(x >= 0 && y >= 0 && x < this.rows && y < this.columns){
          if(this.item[x][y].clicked === false && this.item[x][y].flag === false){
            this.item[x][y].clicked = true
            this.noClick--
            if(this.item[x][y].mines == 0 && this.item[x][y].flag == false){
              this.showBlankItem(x,y)
            }
          }
        }
      }
    }
  },
  //初始化单元格所包含的雷数
  initialNums(){
    for(let i = 0 ; i < this.rows ; i++){
      for(let j = 0 ; j < this.columns ; j++){
        this.item[i][j].mines= this.minesCheck(i,j)
      }
    }
  },
  //展示菜单
  showmenu(type){
    if(type == 0){
      this.showMenu = false
    }else{
      this.showMenu = !this.showMenu
    }
  },
  //切换游戏难度
  changeLevel(i){
    this.$set(this.checkedBox,0,i)
    switch(i){
      case 1:
        this.rows = 9
        this.columns = 9
        this.mines = this.totalMines = 10
        localStorage.setItem("gameLevel","1")
        break;
      case 2:
        this.rows = 16
        this.columns = 16
        this.mines = this.totalMines = 40
        localStorage.setItem("gameLevel","2")
        break;
      case 3:
        this.rows = 16 
        this.columns = 30
        this.mines = this.totalMines = 99
        localStorage.setItem("gameLevel","3")
        break;
      case 4:
        // this.rows = this.mines = 9
        // this.resetGame()
        // break;
    }
    this.resetGame()
  },
  //自定义修改雷数
  changeMines(){
    this.limit()
    localStorage.setItem("custom",`${this.rows},${this.columns},${this.totalMines}`)
    this.resetGame()
    this.toggle(2)
  },
  //限制行数，限制列数，限制雷数
  limit(){
    if(this.rows>=24){
      this.rows = 24
    }else if(this.rows <= 9){
      this.rows = 9
    }
    if(this.columns>=30){
      this.columns = 30
    }else if(this.columns <= 9){
      this.columns = 9
    }
    if(this.totalMines >= 667){
      this.totalMines = 667
    }else if(this.totalMines <= 10){
      this.totalMines = 10
    }
  },
  //开启/关闭声音
  turn(){
    let i = this.checkedBox[1]==5 ? 6 : 5
    this.$set(this.checkedBox,1,i)
  },
  //鼠标移出菜单时隐藏菜单
  hiddenMenu(){
    if(this.showMenu){
      this.$refs.dropdown.removeAttribute("style")
    }
  },
  //移动
  Move(str,index=0){
    let box = document.querySelectorAll(str)[index]
    // box.setCapture&&box.setCapture();
    this.moving = true
    var ol = event.clientX - box.offsetLeft;
    var ot = event.clientY - box.offsetTop;
    document.onmousemove = function(e){
      // var st = document.body.scrollTop || document.documentElement.scrollTop;
      // var sl = document.body.scrollLeft || document.documentElement.scrollLeft; 
      var left = e.clientX;
      var top = e.clientY;
      var boxleft = left - ol
      var boxtop = top - ot
      box.style.left = boxleft > 0? boxleft + "px" : 0;  
      box.style.top = boxtop > 0? boxtop + "px" : 0;
    };
  },
  MoveUp(str,index=0){
    let box = document.querySelectorAll(str)[index]
    document.onmousedown = null;
    document.onmousemove = null;
    // box.releaseCapture&&box.releaseCapture();
    this.moving = false
  },
  //切换排行榜/自定义的显示
  toggle(index){
    if(index == 0){
      this.showRank = !this.showRank
    }
    else if(index == 1){
      this.showCustom = !this.showCustom
    }else{
      this.showCustom = !this.showCustom
      this.checkedBox[0] = 4
      localStorage.setItem("gameLevel","4")
    }
    this.showmenu(0)
  },
  //重置记录
  resetRecord(){
    this.record = [999,999,999]
    this.recordName = ['匿名','匿名','匿名']
    localStorage.setItem("record",'999,999,999')
    localStorage.setItem("recordName",'匿名,匿名,匿名')
  },
  //让弹出框闪烁
  fade(){
    let type = this.showRank ? 0 : this.showCustom ? 1 : null 
    let tip = document.querySelectorAll('.tip')[type]
    tip.classList.add('fade')
    setTimeout(() => {
      tip.classList.remove('fade')
    }, 1000);
  },
  //修改记录
  changeRecord(){
    let value = this.$refs.newRecordInput.value
    this.$set(this.recordName,this.checkedBox[0] - 1,value)
    localStorage.setItem("recordName",`${this.recordName[0]},${this.recordName[1]},${this.recordName[2]}`)
    this.breakRecord = false
    this.toggle(0)
  }
}